﻿<#
.SYNOPSIS  
    Adds and upgrades Project Server 2007 web instances. 
.DESCRIPTION  
    
.NOTES
    If the web instance already exists, it will be skipped.
.LINK  
    http://7up.codeplex.com 
#>


Write-Host ("   Adding Project Web Instances ({0})..." -f [DateTime]::Now.ToString())

#add SharePoint snap in
if ((Get-PSSnapin -Name  Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) 
{ 
    Add-PsSnapin  Microsoft.SharePoint.PowerShell 
}

#get config
$config = [xml](Get-Content Upgrade.xml)

#add project web instances
foreach($psa in $config.Settings.ServiceApplications.ProjectServiceApplications.ProjectServiceApplication)
{
	if($psa -ne $null)
	{
		foreach($pwi in $psa.ProjectWebInstances.ProjectWebInstance)
		{
			$instance = (Get-SPProjectWebInstance -Url $pwi.Url -ErrorAction SilentlyContinue)
			if($instance -eq $null)
			{
				Write-Host ("      Adding '{0}' ({1})..." -f  $pwi.Url, [DateTime]::Now.ToString())
				New-SPProjectWebInstance -Url $pwi.Url -PrimaryDbServer $pwi.PrimaryDbServer -ReportingDbServer $pwi.ReportingDbServer -AdminAccount $pwi.AdminAccount -ArchiveDbname $pwi.ArchiveDbName -DraftDbName $pwi.DraftDbName -PublishedDbName $pwi.PublishedDbName -ReportingDbName $pwi.ReportingDbName -Wait
			}
			else 
			{
				Write-Host ("      Skipping '{0}' because it already exists ({1})." -f $pwi.Url, [DateTime]::Now.ToString())
			}
		}
	}
}
